package com.etc.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.*;
import com.etc.service.impl.MessageServiceImpl;
import com.etc.service1.MessageServiceDao;
import com.etc.util.PageTool;
import com.etc.dao.*;
/**
 * Servlet implementation class MessageServlet
 */
@WebServlet("/MessageServlet")
public class MessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MessageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		int pageNo = 1;
		int pageSize = 10;

		String type = request.getParameter("type");
		//System.out.println("type="+type);
		
		String po=request.getParameter("po");
		//System.out.println("po="+po);
		
		if(po==null||"".equals(po))
		{
			pagesearch(request,response);
		}
		else if(po=="fatie"||po.equals("fatie"))
		{
			fatie(request,response);
		}
		else if(po=="key"||po.equals("key"))
		{
			keysearch(request,response);
		}
		else if(po=="del"||po.equals("del"))
		{
			delmessage(request,response);
		}
		else if(po=="jie"||po.equals("jie"))
		{
			jiemessage(request,response);
		}
		else if(po=="feng"||po.equals("feng"))
		{
			fengmessage(request,response);
		}
		else if(po=="chajin"||po.equals("chajin"))
		{
			chajinmessage(request,response);
		}
		else if(po=="mytie"||po.equals("mytie"))
		{
			mytiemessage(request,response);
		}
		else if(po=="alltie" || po.equals("alltie")) {
			allmessage(request,response);
		}
		else if(po=="fengmes"|| po.equals("fengmes")) {
			fengmes(request,response);
		}
		else if(po=="jiemes"|| po.equals("jiemes")) {
			jiemes(request,response);
		}
		/*
		 * else if(po=="senior"||po.equals("senior")) { senior(request,response); }
		 */
	}
	
	protected void pagesearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		/*
		 * int pageNo = 1; int pageSize = 10;
		 * 
		 * String type = request.getParameter("type"); String strPageNo =
		 * request.getParameter("pageNo"); if(strPageNo != null) { pageNo =
		 * Integer.parseInt(strPageNo); } String search =
		 * request.getParameter("search"); MessageServiceDao dao = new
		 * MessageServiceImpl(); // IMessageDao dao = new MessageDaoImpl();
		 * List<Message> list = new ArrayList<Message>(); try { if(pageNo>0) list =
		 * dao.findAllMessage(pageNo, pageSize,type); } catch (Exception e1) { // TODO
		 * Auto-generated catch block e1.printStackTrace(); }
		 * request.getSession().setAttribute("list", list); int pageCount; try {
		 * pageCount = dao.getPageCount(pageSize);
		 * request.setAttribute("pageCount",pageCount);
		 * request.setAttribute("pageNo",pageNo);
		 * request.getSession().setAttribute("search", search);
		 * request.getRequestDispatcher("tourist.jsp").forward(request, response); }
		 * catch (SQLException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */
		String s = request.getParameter("pageNo");
		String type= request.getParameter("type");
		int pageNo=Integer.parseInt(s);
		int pagesize=10;
		MessageServiceDao dao = new MessageServiceImpl();
		try {
			List<Message> list = dao.findAllMessage(pageNo, pagesize, type);
			PageTool<Message> pageMessage = new PageTool<Message>();
			pageMessage.setList(list);
			pageMessage.setPageIndex(pageNo);
			pageMessage.setPageSize(pagesize);
			
			int result = dao.getPageCount(pagesize);
			pageMessage.setTotalCount(result);
			request.setAttribute("searchType", type);
			request.setAttribute("pageMessage", pageMessage);
			request.getRequestDispatcher("tourist.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void keysearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
/*		int pageNo = 1;
		int pageSize = 10;*/
		/*
		 * String strPageNo = request.getParameter("pageNo"); if(strPageNo != null) {
		 * pageNo = Integer.parseInt(strPageNo); }
		 */
	   int pageindex=Integer.valueOf(request.getParameter("pageIndex"));
	   int pagesize=10;
		String search = request.getParameter("search");
		
		MessageServiceDao dao = new MessageServiceImpl();
		List<Message> list = new ArrayList<Message>();
	
			if(pageindex>0)
			list = dao.searchKeyMessage(search);
			PageTool<Message> pageMessage = new PageTool<Message>();
			pageMessage.setList(list);
			pageMessage.setPageIndex(pageindex);		
			pageMessage.setPageSize(pagesize);
			Long result=dao.countAll();
			pageMessage.setTotalCount(result.intValue());

		request.setAttribute("listMessage", pageMessage);
		request.getSession().setAttribute("search", search);
	    request.getRequestDispatcher("message-list.jsp").forward(request, response); 
		/*
		 * int pageCount; try { pageCount = dao.getPageCount(pageSize);
		 * request.setAttribute("pageCount",pageCount);
		 * request.setAttribute("pageNo",pageNo);
		 * request.getSession().setAttribute("search", search);
		 * request.getRequestDispatcher("tourist.jsp").forward(request, response); }
		 * catch (SQLException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */
		
	}
	
	//ɾ��
	protected void delmessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		String mytietip = request.getParameter("mytietip");
		System.out.println(mytietip);
		String title = request.getParameter("title");
		int id= Integer.parseInt(request.getParameter("id"));
		MessageServiceDao dao = new MessageServiceImpl();
		if(dao.deleteMessage(title)||dao.deleteAnsMessage(title)||dao.deleteAnsAnsMessage(title))
		{
				if(mytietip=="mytie"||"mytie".equals(mytietip))   
				{
					request.getSession().setAttribute("mytietip", null);
					request.getRequestDispatcher("user_show.jsp").forward(request, response);
				}
				else 
				{
					pagesearch(request,response);				
				}
		}
		else
		{
			System.out.println("del-no");
		}
	}
	
	//�����
	protected void jiemessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		
		int id= Integer.parseInt(request.getParameter("id"));
		String username1 =(String) request.getSession().getAttribute("username");
		String type=request.getParameter("type");
		MessageServiceDao dao = new MessageServiceImpl();
		if(dao.openMessage(id))
		{
			/* mytiemessage(request,response); */
			if(type.equals("my")) {
			request.getRequestDispatcher("MessageServlet?po=mytie&pageIndex=1&username="+username1).forward(request, response);
			}
			else {
			request.getRequestDispatcher("MessageServlet?po=alltie&pageIndex=1").forward(request, response);
			}
		}
		else
		{
			System.out.println("jie-no");
		}
	}
	protected void jiemes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		
		int id= Integer.parseInt(request.getParameter("id"));
		String username1 =(String) request.getSession().getAttribute("username");
		String type=request.getParameter("type");
		MessageServiceDao dao = new MessageServiceImpl();
		if(dao.openMessage(id))
		{
			/* mytiemessage(request,response); */
			request.getRequestDispatcher("ListMessageServlet?pageIndex=1").forward(request, response);
		}
		else
		{
			System.out.println("jie-no");
		}
	}
	//�����
	protected void fengmessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		
		int id= Integer.parseInt(request.getParameter("id"));
		String username1 =(String) request.getSession().getAttribute("username");
		String type=request.getParameter("type");
		MessageServiceDao dao = new MessageServiceImpl();
		if(dao.closeMessage(id))
		{
			//request.getRequestDispatcher("MessageServlet").forward(request, response);
			if(type.equals("my")) {
			request.getRequestDispatcher("MessageServlet?po=mytie&pageIndex=1&username="+username1).forward(request, response);
			}
			else {
				request.getRequestDispatcher("MessageServlet?po=alltie&pageIndex=1").forward(request, response);
			}
		}
		else
		{
			System.out.println("feng-no");
		}
	}
	protected void fengmes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		
		int id= Integer.parseInt(request.getParameter("id"));
		String username1 =(String) request.getSession().getAttribute("username");
		String type=request.getParameter("type");
		MessageServiceDao dao = new MessageServiceImpl();
		if(dao.closeMessage(id))
		{
			//request.getRequestDispatcher("MessageServlet").forward(request, response);
			request.getRequestDispatcher("ListMessageServlet?pageIndex=1").forward(request, response);
		}
		else
		{
			System.out.println("feng-no");
		}
	}
	protected void chajinmessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
	
		MessageServiceDao dao = new MessageServiceImpl();
		List<Message> list = new ArrayList<Message>();
		list=dao.findjinMessage();
		request.getSession().setAttribute("list", list);
		request.getSession().setAttribute("jin_info", "chajin");
		request.getRequestDispatcher("chajin_message.jsp").forward(request, response);
	}

	protected void mytiemessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
	
		String username = request.getParameter("username");
		int pageindex=Integer.valueOf(request.getParameter("pageIndex"));
		int pagesize = 10;
		MessageServiceDao ms = new MessageServiceImpl();
		/* List<Message> list = ms.searchAllMessage(pageindex,pagesize,type); */ 
		List<Message> list = ms.myMessage(username,pageindex,pagesize);
		PageTool<Message> pageMessage = new PageTool<Message>();
		pageMessage.setList(list);
		pageMessage.setPageIndex(pageindex);		
		pageMessage.setPageSize(pagesize);
		Long result=ms.countAll();
		pageMessage.setTotalCount(result.intValue());
		
		request.setAttribute("list", pageMessage);
	    request.getRequestDispatcher("html/mymessage-list.jsp").forward(request, response);
	}
	protected void allmessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
	
		String username = request.getParameter("username");
		int pageindex=Integer.valueOf(request.getParameter("pageIndex"));
		int pagesize = 10;
		MessageServiceDao ms = new MessageServiceImpl();
		/* List<Message> list = ms.searchAllMessage(pageindex,pagesize,type); */ 
		List<Message> list = ms .myMessage(username,pageindex,pagesize);
		PageTool<Message> pageMessage = new PageTool<Message>();
		pageMessage.setList(list);
		pageMessage.setPageIndex(pageindex);		
		pageMessage.setPageSize(pagesize);
		Long result=ms.countAll();
		pageMessage.setTotalCount(result.intValue());
		
		request.setAttribute("list", pageMessage);
		//request.getSession().setAttribute("mytietip", "mytie");
		request.getRequestDispatcher("html/allmessage-list.jsp").forward(request, response);
	}
	protected void fatie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		String username = request.getParameter("username");
		String title = request.getParameter("title");
		String type = request.getParameter("type");
		String content = request.getParameter("content");
		
		Message message = new Message();
		message.setTitle(title);
		message.setUsername(username);
		message.setType(type);
		message.setContent(content);
		
		MessageServiceDao dao = new MessageServiceImpl();
		System.out.println(message.getTitle()+message.getUsername()+message.getType()+message.getContent());
		if(dao.insertMessage(message))
		{
			mytiemessage(request,response);
		}
		else
		{
			request.setAttribute("fatieinfo","发布失败");
			request.getRequestDispatcher("fa_message.jsp").forward(request,response);
		}
	}
	
	/*
	 * //�߼����� protected void senior(HttpServletRequest request,
	 * HttpServletResponse response) throws ServletException, IOException { // TODO
	 * Auto-generated method stub request.setCharacterEncoding("UTF-8");
	 * response.setContentType("text/html;charset=utf-8");
	 * 
	 * String username = request.getParameter("username"); String searchtext =
	 * request.getParameter("searchtext"); String types =
	 * request.getParameter("types"); String status =
	 * request.getParameter("status"); String sort = request.getParameter("sort");
	 * String sj = request.getParameter("sj");
	 * 
	 * MessageServiceDao dao = new MessageServiceImpl(); List<Message> list = new
	 * ArrayList<Message>(); try { list=dao.searchHighMessage(searchtext, types,
	 * status, sort, sj); } catch (SQLException e) { // TODO Auto-generated catch
	 * block e.printStackTrace(); } //System.out.print(list.size());
	 * request.setAttribute("searchtext", searchtext); request.setAttribute("types",
	 * types); request.setAttribute("status", status); request.setAttribute("sort",
	 * sort); request.setAttribute("sj", sj);
	 * request.getSession().setAttribute("seniorlist", list);
	 * request.getRequestDispatcher("Senior_show.jsp").forward(request, response);
	 * 
	 * }
	 */
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
